<template>
  <div class="dialog">
    <el-dialog width="45%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="行业名称：" prop="industryName">
          <el-col :span="12">
            <el-input v-model="form.industryName" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="行业排序：" prop="industrySort">
          <el-col :span="12">
            <el-input v-model="form.industrySort" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="产品版本：" prop="productType">
          <el-col :span="12">
            <el-select v-model="form.productType" placeholder="请选择" clearable>
              <el-option
                v-for="item in productTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="行业图片：" prop="industryImg">
          <el-col :span="12">
            <UploadImg :file-list="form.industryImg" @change="uploadHandle($event, 'industryImg')" />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { PRODUCT_TYPE } from '@/utils/enum'
import UploadImg from '@/components/UploadFile/index.vue'
export default {
  name: 'AddIndustry',
  components: {
    UploadImg
  },
  props: {
    formDetail: {
      type: Object,
      default: () => ({})
    }
  },

  data() {
    return {
      loading: false,
      productTypeOptions: PRODUCT_TYPE,
      form: {
        industryName: '',
        industrySort: '',
        productType: '',
        industryImg: ''
      },
      rules: {
        industryName: [{ required: true, message: '请输入行业名称', trigger: ['blur'] }],
        productType: [{ required: true, message: '请选择产品版本', trigger: ['change'] }],
        industryImg: [{ required: true, message: '请上传行业图片', trigger: ['change'] }]
      },
      imgList: []
    }
  },

  computed: {
    title() {
      if (this.formDetail.industryCode) {
        return '编辑行业'
      } else {
        return '新增行业'
      }
    }
  },

  created() {
    const { industryName, industrySort, productType, industryImg } = this.formDetail
    this.form = {
      industryName,
      industrySort,
      productType,
      industryImg
    }
  },

  methods: {

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      if (this.formDetail.industryCode) {
        this.editHandle()
      } else {
        this.addHandle()
      }
    },

    // 新增
    addHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form
          }
          console.log(data)
          // addUser(data).then(() => {
          //   this.$message.success('新增成功')
          //   this.loading = false
          //   this.$emit('confirm')
          //   this.close()
          // }).catch(() => {
          //   this.loading = false
          // })
        } else {
          return false
        }
      })
    },

    // 编辑
    editHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form,
            id: this.formDetail.industryCode
          }
          console.log(data)
          // editUser(data).then(() => {
          //   this.$message.success('编辑成功')
          //   this.loading = false
          //   this.$emit('confirm')
          //   this.close()
          // }).catch(() => {
          //   this.loading = false
          // })
        } else {
          return false
        }
      })
    },

    // 上传图片处理
    uploadHandle(payload, name) {
      this.form[name] = payload.fileList[0]?.url
      this.$refs.form.validateField(name)
    }

  }

}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
</style>
